<%@ page language="java" import="okhttp3.OkHttpClient" pageEncoding="UTF-8" %>
<%@ page import="okhttp3.Request" %>
<%@ page import="okhttp3.Response" %>
<%@ page import="java.io.File" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.util.concurrent.TimeUnit" %>
<%--流程退回调用QMS接口--%>
<%
    String itemNumber = "TEST01";
    String result = sendOKHttpRequest(itemNumber);
    out.println(">>>>>测试接口>>>>>result=" + result + "<br/>");
%>

<%!

    private static final String url = "http://10.196.80.148:8012/ExternalData/getPdf";

    public static String sendOKHttpRequest(String params) {
         String result = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            Request request = new Request.Builder()
                    .url(url + "?itemNumber=" + params)
                    .get()
                    .build();
            Response response = client.newCall(request).execute();
            InputStream inputStream = response.body().byteStream();
            // 将输入流转换为文件
            convert(inputStream, "/usr/weaver/ecology/temp/test.pdf");
            result = "文件转换成功！";
        } catch (Exception e) {
            result = "文件转换失败";
            e.printStackTrace();
        }
        return result;
    }
%>

<%!

    public static void convert(InputStream inputStream, String filePath) {
        try {
            File file = new File(filePath);
            FileOutputStream outputStream = new FileOutputStream(file);

            int bytesRead;
            byte[] buffer = new byte[1024];

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            outputStream.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
%>


